//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var app = getApp();

    if (app.globalData.openid == null) {
      console.log("转入授权")
      this.getset()
    }
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    var testUrl = app.testUrl;
    var that = this;
    wx.request({
      url: testUrl + '/user/search.do?page=1&rows=10',
      method: "POST",
      data: {
        wxOpenid: app.globalData.openid,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data.rows)
        that.setData({
          user: res.data.rows[0]
        });
      }
    });

  },
  // 获取设置信息
  getset: function () {

    var user = { info: {} }
    // 获取用户信息
    wx.getSetting({
      success: function (res) {
        console.log("获取设置信息成功")
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log("获取用户信息成功")
              // var that = getApp();
              console.log(res.userInfo)
              user.info.userinfo = res.userInfo
              // 登录
              wx.login({
                success: res => {
                  console.log(res)
                  // var that = getApp();
                  // that.code = res.code
                  user.code = res.code
                  var app = getApp()
                  var testurl = app.testUrl
                  wx.request({
                    url: testurl + '/user/getcode.do?code=',
                    data: {
                      code: res.code
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                      var app = getApp()
                      console.log(res)
                      user.info.openid = res.data.openid
                      user.info.userinfo.area = app.getc.getChina(user.info.userinfo.province) + " " + app.getc.getChina(user.info.userinfo.city),
                        console.log(user)
                      wx.setStorage({
                        key: "user",
                        data: user.info
                      })

                      // 设置用户信息
                      app.globalData.userInfo = user.info.userinfo
                      app.globalData.openid = user.info.openid
                      wx.request({
                        url: testurl + '/user/add.do',
                        method: "POST",
                        data: {
                          wxOpenid: user.info.openid,
                          wxUsername: user.info.userinfo.nickName,
                          userImg: user.info.userinfo.avatarUrl,
                          area: user.info.userinfo.area,
                          username: user.info.userinfo.nickName
                        },
                        header: {
                          'content-type': 'application/json' // 默认值
                        },
                        success: function (res) {
                          console.log(res)
                          var app = getApp()
                          console.log(app)
                        }
                      })

                    }
                  })
                  // 发送 res.code 到后台换取 openId, sessionKey, unionId
                },
                fail: function (res) {
                  console.log(res + "失败")
                }
              })


            },
            fail: function (e) {
              console.log(e + "获取用户信息失败")

            }
          })
        } else {
          console.log(111)
          console.log('没有获取授权')
          wx.redirectTo({
            url: '/pages/msg/msg_fail?page=3'
          })

        }
      },
      fail: function () {
        console.log("获取设置信息失败")
      }
    })


  },
  tomyclass: function (e) {
    wx.navigateTo({
      url: '/pages/myclass/myclass',
    })
  },

  tomyshare: function (e) {
    wx.navigateTo({
      url: '/pages/myshare/myshare',
    })
  },
  tomycomment: function (e) {
    wx.navigateTo({
      url: '/pages/mycomment/mycomment',
    })
  },

  tomessage: function (e) {
    wx.navigateTo({
      url: '/pages/messages/messages',
    })
  },
  toedit: function (e) {
    wx.navigateTo({
      url: '/pages/edit/edit',
    })
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})